var data=require("../../utiliz/data.js");
var show = require("../../utiliz/show.js")
var mapClick = require("../../utiliz/click.js")
var mouses = require("../../utiliz/mouse.js");
var cats = require("../../utiliz/cat.js");
var blocks = require("../../utiliz/block.js");
var traps = require("../../utiliz/trap.js");
var bombs = require("../../utiliz/boom.js");
var gas = require("../../utiliz/gas.js")
var light = require("../../utiliz/light.js");

var ctx;
var flashtimes = 0;
var maxTime = 120;

var maxCat = 5;
var maxWall = 50;
var maxTrap = 50;
var maxBomb = 50;
var maxGas = 50;

var usedCat = 0;
var usedWall =0;
var usedTrap = 0;
var usedBomb =0;
var usedGas = 0;


//////////////////////////////////////////////////////
var mapW = 30;
var mapH = 40;
var width = 750 / mapW;
var map = [];
var mapMouse = [];



Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: true,
    tcat:"cat",
    twall:"wall",
    ttrap:"trap",
    tbomb:"bomb",
    tgas:"gas",
    time:"start"
  },
  init: function (col, row){
    if(col==31&&row==40)
    {
      maxCat = 100;
      maxWall = 500;
      maxTrap = 500;
      maxBomb = 500;
      maxGas = 500;
    }
    mapH=row;
    mapW=col;
    width = 750 / mapW;
    map = new Array(mapH);
    mapMouse = new Array(mapH);
    for (var i = 0; i < mapH; i++) {
      map[i] = new Array(mapW);
      mapMouse[i] = new Array(mapW);
      for (var j = 0; j < mapW; j++) {
        map[i][j] = 0;
        mapMouse[i][j] = -1;
        if (i == 0 || j == 0 || i == mapW - 1 || j == mapW - 1)
          map[i][j] = 1;
      }
    }
  },
  clear: function(){
    for (var i = 0; i < mapH; i++) {
      for (var j = 0; j < mapW; j++) {
        map[i][j] = 0;
        mapMouse[i][j] = -1;
        if (i == 0 || j == 0 || i == mapW - 1 || j == mapW - 1)
          map[i][j] = 1;
      }
    }
  },

  startPlay: function () {
    show.initMap(map, mapW, mapH, width, mapMouse);
    mouses.createMouses(parseInt(mapW * mapH / 20), map, mapW, mapH, width, mapMouse);
    //parseInt(mapW * mapH / 20)
    this.timer = setInterval(this.drawImg, data.define.ShowDelay); //
    this.setData({
      flag: false
    })
  },
  stopPlay: function () {
    clearInterval(this.timer);
    this.setData({
      flag: true
    })
  },
  drawImg: function () {
    flashtimes++;
    var temp = maxTime - flashtimes * data.define.ShowDelay / 1000;
    
    if(temp<=0)
    {
      flashtimes=0;
      mouses.createMouses(parseInt(mapW*mapH/10), map, mapW, mapH, width, mapMouse);
      temp=maxTime;
    }

    if (mouses.getMouseNumber() == 0) {
      clearInterval(this.timer);
      this.setData({
        time: "Win!"
      })
      return ;
    }

    if(parseInt(temp)%4 > 1)
    {
      this.setData({
        tcat:(maxCat-usedCat).toFixed(),
        ttrap: (maxTrap - usedTrap).toFixed(),
        tbomb: (maxBomb - usedBomb).toFixed(),
        tgas: (maxGas - usedGas).toFixed(),
        twall: (maxWall - usedWall).toFixed(),
        time: "m:"+mouses.getMouseNumber().toFixed()
      })
    }
    else
    {
      this.setData({
        tcat: "cat",
        twall: "wall",
        ttrap: "trap",
        tbomb: "bomb",
        tgas: "gas",
        time: (temp).toFixed()
      })
    }

    
    // if (flashtimes % data.define.MouseSpeed==0)
    mouses.updateMouses(flashtimes % data.define.MouseSpeed, map, mapW, mapH, width, mapMouse);

    if (flashtimes % data.define.CatSpeed)
      cats.updateCats(0, map, mapW, mapH, width, mapMouse);

    blocks.updateBlocks(map, mapW, mapH, width, mapMouse);
    traps.updateTraps(map, mapW, mapH, width, mapMouse);
    bombs.updateBooms(map, mapW, mapH, width, mapMouse);
    gas.updateGAS(map, mapW, mapH, width, mapMouse);
    light.updateLight(map, mapW, mapH, width, mapMouse);

    let map_temp = show.showMap(map, mapW, mapH, width, mapMouse);
    var w = width / 2;
    var h = width / 2;
    for (var i = 0; i < map_temp.length; i++) {
      if (map_temp[i].flag != 0)
        ctx.drawImage(map_temp[i].url, map_temp[i].x, map_temp[i].y , w, h);
    }
    ctx.draw(false)
  },

  tapMap: function(e){
    var x = (parseInt(e.detail.y) / width * 2).toFixed();
    var y = (parseInt(e.detail.x) / width * 2).toFixed();
    mapClick.setCursor(x, y, map, mapW, mapH, width, mapMouse);
  },

  addCat: function () {
    if(maxCat-usedCat>0)
    {
      usedCat++;
      cats.createCat(map, mapW, mapH, width, mapMouse);
    }  
    
  },
  addBlock: function () {
    if (maxWall - usedWall>0)
    {
      usedWall++;
      blocks.createBlock(map, mapW, mapH, width, mapMouse);
    }  
    
  },
  addTrap: function () {
    if (maxTrap - usedTrap>0)
    {
      usedTrap++;
      traps.createTrap(map, mapW, mapH, width, mapMouse);
    }  
    
  },
  addBoom: function () {
    if (maxBomb - usedTrap>0)
    {
      usedTrap++;
      bombs.createBoom(map, mapW, mapH, width, mapMouse);
    }  
    
  },
  addGas: function () {
    if (maxGas - usedGas>0)
    {
      usedGas++;
      gas.createGAS(map, mapW, mapH, width, mapMouse);
    }  
    
  },
  addLight: function(){
    light.createLight(map, mapW, mapH, width, mapMouse);
  },


  onLoad: function (options) {
    if(options.type=="init")
    {
      if (this.timer)
        clearInterval(this.timer);
      
      show.clear();
      mouses.clear();
      mapClick.clear();
      gas.clear();
      cats.clear();
      blocks.clear();
      traps.clear();
      flashtimes = 0;

      let col = parseInt(options.col);
      let row = parseInt(options.row);
      this.init(col, row);

      usedCat = 0;
      usedWall = 0;
      usedTrap = 0;
      usedBomb = 0;
      usedGas = 0;

      this.setData({
        flag: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    ctx = wx.createCanvasContext("canvas1");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})